﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace DevWeb.BizTalk.BTSTemplate.Setup.Installer
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {

        private const string BTDFEXTENSIONPATH = "BTDFEXTENSIONPATH";
        private const string BTDFENV = "BTDFENV";
        private const string BTDFMANAGEMENTSERVER ="BTDFMANAGEMENTSERVER";
        private const string BTDFDEVSERVICEURL = "BTDFDEVSERVICEURL";
        private const string BTDFCENTRALSERVICEURL = "BTDFCENTRALSERVICEURL";
        private const string BTDFCOMPANYNAME = "BTDFCOMPANYNAME";

        public Installer()
        {
            InitializeComponent();
        }

        
        public override void Install(IDictionary stateSaver)
        {


           
            base.Install(stateSaver);
        }


        /// <summary>
        /// setup the envr vars into the system
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnAfterInstall(IDictionary savedState)
        {

#if DEBUG

            Debugger.Launch();
#endif


            string serviceurl = this.Context.Parameters[BTDFDEVSERVICEURL];
            DoesURLExist(serviceurl);
            string centralserviceurl = this.Context.Parameters[BTDFCENTRALSERVICEURL];
            DoesURLExist(centralserviceurl);

            string targetDir = this.Context.Parameters["targetdir"];
            string environment = this.Context.Parameters[BTDFENV];
            string managementserver = (this.Context.Parameters[BTDFMANAGEMENTSERVER] == "YES") ? "true" : "false";
            string devserviceurl = this.Context.Parameters[BTDFDEVSERVICEURL];

            string companyname = this.Context.Parameters[BTDFCOMPANYNAME];

            companyname = companyname.Replace(" ", "");

            
            


            //if not exists, create it
            if (!Directory.Exists(targetDir))
            {
                try
                {
                    var di = Directory.CreateDirectory(targetDir);
                }
                catch (Exception ex)
                {

                    throw ex;
                }


            }


            Environment.SetEnvironmentVariable(BTDFEXTENSIONPATH, targetDir, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFENV, environment, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("BT_DEPLOY_MGMT_DB", managementserver, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFDEVSERVICEURL, devserviceurl, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFCENTRALSERVICEURL, centralserviceurl, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFEXTENSIONPATH, targetDir, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFCOMPANYNAME, companyname, EnvironmentVariableTarget.Machine);
            base.OnAfterInstall(savedState);
        }

        protected override void OnAfterUninstall(IDictionary savedState)
        {




            Environment.SetEnvironmentVariable(BTDFEXTENSIONPATH, null, EnvironmentVariableTarget.Machine);

            Environment.SetEnvironmentVariable(BTDFENV, null, EnvironmentVariableTarget.Machine); 
            Environment.SetEnvironmentVariable(BTDFMANAGEMENTSERVER, null, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFDEVSERVICEURL, null, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFCENTRALSERVICEURL, null, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable(BTDFCOMPANYNAME, null, EnvironmentVariableTarget.Machine);
        

           
            
            
            base.OnAfterUninstall(savedState);
        }


        public  void DoesURLExist(string url)
        {
            try
            {
                WebRequest req = WebRequest.Create(url);

                WebResponse res = req.GetResponse();

                Console.WriteLine("Url Exists");
            }
            catch (WebException ex)
            {
                var MSG = String.Format("Checking URL : {0} results in exception {1}", url, ex.Message);
               WriteLogEntry(MSG, EventLogEntryType.Error);
            }
        }


        private void WriteLogEntry(string message, EventLogEntryType type)
        {
            var msg = string.Format("Installer by {0} : {1}", Environment.UserName, message);

            EventLog.WriteEntry("Application", msg, type);
        }
    }
}
